package com.example.controller;

import com.example.repo.PersonPage;
import com.example.service.PageService;
import com.example.service.PersonService;
import com.example.vo.Person;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by 000284 on 2017/1/10.
 */
@Controller
@RequestMapping("/person")
public class PersonController {

    @Resource
    public PersonService personService;
    @Resource
    public PageService pageService;

    @RequestMapping("/add")
    @ResponseBody
    public Person add(){
        Person person = new Person("张三",10);
        personService.save(person);
        return person;
    }
    @ResponseBody
    @RequestMapping("/delete")
    public void delete(int id){
        personService.delete(id);
    }

    @ResponseBody
    @RequestMapping("/getall")
    public Iterable<Person> get(){
        return personService.findAll();
    }
    @RequestMapping("/params")
    @ResponseBody
    public Iterable<Person> params(@RequestParam(value = "page",defaultValue = "0") Integer page,
                                   @RequestParam(value = "size",defaultValue = "10") Integer size){
        Sort sort = new Sort(Sort.Direction.DESC,"id");
        Pageable pageable = new PageRequest(page,size,sort);
        return pageService.findAll(pageable);
    }
}
